package com.dk.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.dk.api.IntelligentFeignApi;
import com.dk.constant.CollectionConstant;

import com.dk.mapper.DicRecordMapper;
import com.dk.mapper.TbCarsMapper;

import com.dk.pojo.DicRecord;
import com.dk.pojo.TbCars;
import com.dk.service.DicRecordService;

import com.dk.vo.DicRecordVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class DicRecordServiceImpl implements DicRecordService {

    @Resource
    private DicRecordMapper recordMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Resource
    private TbCarsMapper tbCarsMapper;




    @Override
    public List<DicRecordVO> getCarDicRecord(Long cids) {

        LambdaQueryWrapper<DicRecord> wrapper = Wrappers.<DicRecord>lambdaQuery().eq(!StringUtils.isEmpty(cids.toString()),
                DicRecord::getId, cids);
        List<DicRecord> dicRecordList = recordMapper.selectList(wrapper);

        List<DicRecordVO> voList = new ArrayList<>();
        dicRecordList.forEach(i -> {
            DicRecordVO vo = new DicRecordVO();
            BeanUtils.copyProperties(i, vo);
            voList.add(vo);
        });
        //将id转换成string
        String s = String.valueOf(cids);

        TbCars tbCars = tbCarsMapper.selectById(cids);
        redisTemplate.boundHashOps(CollectionConstant.CAR).put(CollectionConstant.CAR_ID+"-"+cids,tbCars);


        // 人气榜 点击自增一  存到redis 增加分数

        redisTemplate.opsForZSet().incrementScore(CollectionConstant.POPULAR_LIST,s,1);

        return voList;
    }


}
